package leetcode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;

public class tentwo {
    public static int lengthOfLongestSubstring(String s) {
        if(s==null||s.length()==0){
            return 0;
        }
        HashSet<Character> set=new HashSet<>();
        List<Integer> list=new ArrayList<>();
        int left;
        int right;
        int n=s.length();
        for(right=0,left=0;right<n;right++){
            char c=s.charAt(right);
            if(!set.contains(c)){
                set.add(c);
            }else{
                list.add(right-left);
                while(set.contains(c)){
                    set.remove(s.charAt(left));
                    left++;
                }
                set.add(c);
            }
        }
        //如果全程没有重复的，则就进不了else，则可能list里面可能为空
        list.add(n-left);
        return Collections.max(list);
    }

    public static void main(String[] args) {
        String string="deabcabca";
        int i = lengthOfLongestSubstring(string);
        System.out.println(i);

    }
}
